﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AtDevice;

namespace Mokkula
{
    public partial class TextMessageNewForm : Form
    {
        /// <summary>
        /// Construct new form for sending a message.
        /// </summary>
        /// <param name="number">Target number</param>
        /// <param name="message">Message</param>
        public TextMessageNewForm(string number, string message)
        {
            InitializeComponent();
            targetTextBox.Text = number;
            messageTextBox.Text = message;
            UpdateCounter();
        }

        private void UpdateCounter()
        {
            this.statusLabel.Text = String.Format("{0} characters", this.messageTextBox.Text.Length);
        }

        private void sendButton_Click(object sender, EventArgs e)
        {
            DeviceHandler.Get().GetSMSInteface().SendSMS(targetTextBox.Text, messageTextBox.Text);
            this.statusLabel.Text = "Message sent!";
            CloseForm(400);
        }

        private void CloseForm(int interval)
        {
            Timer t = new Timer();
            t.Tick += delegate { this.Close(); };
            t.Interval = interval;
            t.Start();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            ISMSInterface smsi = DeviceHandler.Get().GetSMSInteface();
            smsi.SaveSMS(targetTextBox.Text, messageTextBox.Text);

            this.statusLabel.Text = "Message saved!";
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void targetTextBox_TextChanged(object sender, EventArgs e)
        {
            if (targetTextBox.Text.Length > 0)
                sendButton.Enabled = true;
            else
                sendButton.Enabled = false;
        }

        private void messageTextBox_TextChanged(object sender, EventArgs e)
        {
            UpdateCounter();
        }

        private void TextMessageNewForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }
    }
}
